home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / ex6-1.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  4KB  |  156 lines

  1. // ex6-1.c -- Geometry class hierarchy
  2.  
  3. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex6-1.c,v 3.0 90/05/15 22:45:39 kgorlen Rel $
  4.  
  5. #include <iostream.h>
  6. #include <osfcn.h>
  7.  
  8. class Point {
  9.     int xc,yc;      // x-y coordinates
  10. public:
  11.     Point()                     { xc = yc = 0; }
  12.     Point(int newx, int newy)   { xc=newx; yc=newy; }
  13.     int x() const               { return xc; }
  14.     int x(int newx)             { return xc = newx; }
  15.     int y() const               { return yc; }
  16.     int y(int newy)             { return yc = newy; }
  17.     Point operator+(const Point& p) const {
  18.         return Point(xc+p.xc, yc+p.yc);
  19.     }
  20.     void operator+=(const Point& p) {
  21.         xc += p.x();
  22.         yc += p.y();
  23.     }
  24.     void printOn(ostream& strm=cout) const;
  25. };
  26.  
  27. void Point::printOn(ostream& strm) const
  28. {
  29.     strm << '(' << xc << ',' << yc << ')';
  30. }
  31.  
  32. ostream& operator<<(ostream& strm, const Point& p) 
  33. {
  34.     p.printOn(strm);
  35.     return strm;
  36. }
  37.  
  38. class TransformStack {
  39.     Point s[100];   // array to hold stack of points
  40.     Point* top;     // current top of stack
  41. public:
  42.     TransformStack()        { top = s; }
  43.     Point* current() const  { return top; }
  44.     void push(Point& p) {
  45.         Point newtop = *top + p;
  46.         *++top = newtop;
  47.     }
  48.     void pop()              { top--; }
  49. };
  50.  
  51. TransformStack transform;   // shape translation stack
  52.  
  53. class Shape {
  54. public:
  55.     virtual void move(const Point&) =0;
  56.     virtual void draw() const =0;
  57. };
  58.  
  59. class Line: public Shape {
  60.     Point org;      // origin
  61.     Point p;        // end point
  62. public:
  63.     Line(const Point& a, const Point& b) : org(a), p(b) {}
  64.     virtual void move(const Point&);
  65.     virtual void draw() const;
  66. };
  67.  
  68. void Line::move(const Point& d)
  69. {
  70.     org += d;
  71.     p += d;
  72. }
  73.  
  74. void Line::draw() const
  75. {
  76.     cout << "Line from " << *transform.current() + org
  77.         << " to " << *transform.current() + p << endl;
  78. }
  79.  
  80. class Circle: public Shape {
  81.     Point org;      // origin
  82.     int rad;        // radius of circle
  83. public:
  84.     Circle(const Point& c, int r) : org(c) { rad = r; }
  85.     virtual void move(const Point&);
  86.     virtual void draw() const;
  87. };
  88.  
  89. void Circle::move(const Point& d)
  90. {
  91.     org += d;
  92. }
  93.  
  94. void Circle::draw() const
  95. {
  96.     cout << "Circle with center " << *transform.current() + org
  97.         << " and radius " << rad << endl;
  98. }
  99.  
  100. const unsigned PICTURE_CAPACITY = 100;
  101.  
  102. class Picture: public Shape {
  103.     Point org;                  // origin
  104.     Shape* s[PICTURE_CAPACITY]; // array of pointers to shapes
  105.     int n;                      // number of shapes in this Picture
  106. public:
  107.     Picture() : org(0,0)        { n = 0; }  // constructor
  108.     Picture(Point& o) : org(o)  { n = 0; }  // constructor
  109.     void add(Shape&);                       // add Shape to Picture
  110.     virtual void move(const Point& d)   { org += d; }
  111.     virtual void draw() const;              // draw picture;
  112. };
  113.  
  114. void Picture::add(Shape& t)
  115. {
  116.     if (n == PICTURE_CAPACITY) {
  117.         cerr << "Picture capacity exceeded\n";
  118.         exit(1);
  119.     }
  120.     s[n++] = &t;        // add pointer to Shape to Picture
  121. }
  122.  
  123. void Picture::draw() const  // draw a Picture
  124. {
  125.     transform.push(org);
  126.     for (int i=0; i<n; i++) s[i]->draw();
  127.     transform.pop();
  128. }
  129.  
  130. main()
  131. {
  132.     Line l(Point(1,2),Point(3,4));  // create a Line
  133.     Circle c(Point(5,6),1);         // create a Circle
  134.     l.draw();                       // draw the line
  135.     c.draw();                       // draw the circle
  136.     Picture p;                      // create an empty Picture
  137.     l.move(Point(1,0));             // move the line
  138.     p.add(l);                       // add the line to the picture
  139.     c.move(Point(1,0));             // move the circle
  140.     p.add(c);                       // add it to the picture
  141.     p.draw();                       // draw the picture
  142.     p.move(Point(10,10));           // translate it by (10,10)
  143.     p.draw();                       // draw it again
  144.  
  145.     Picture bigPic;
  146.     Picture littlePic1, littlePic2(*new Point(10,10));
  147.     littlePic1.add(*new Line(Point(1,1),Point(2,2)));
  148.     littlePic1.add(*new Circle(Point(3,3),1));
  149.     littlePic2.add(*new Line(Point(4,4),Point(5,5)));
  150.     littlePic2.add(*new Circle(Point(3,3),2));
  151.     littlePic2.move(Point(1,1));
  152.     bigPic.add(littlePic1);
  153.     bigPic.add(littlePic2);
  154.     bigPic.draw();
  155. }
  156.